package acwing._2LanQiao._6DoublePointer_BFS_Graph;
/**
 * @Project : ProgrammingExercises
 * @Package : ACWing._2LanQiao._6DoublePointer_BFS_Graph
 * @File : Q1207.java
 * @Author : WangRuoyu
 * @Date : 2023/3/19 20:29
 */

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;

public class Q1207 {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] words = br.readLine().split(" ");
        int n = Integer.parseInt(words[0]);

        ArrayList<ArrayList<int[]>> path = new ArrayList<>();
        for (int i = 0; i < n; ++i) {
            path.add(new ArrayList<>());
        }
        for (int i = 0; i < n - 1; ++i) {
            words = br.readLine().split(" ");
            int x;
            int y;
            int c;
            if (words.length == 3) {
                x = Integer.parseInt(words[0]);
                y = Integer.parseInt(words[1]);
                c = Integer.parseInt(words[2]);
            } else {
                x = Integer.parseInt(words[0]);
                y = Integer.parseInt(words[2]);
                c = Integer.parseInt(words[4]);
            }
            path.get(x - 1).add(new int[]{y - 1, c});
            path.get(y - 1).add(new int[]{x - 1, c});
        }

        long[] visited = new long[n];

        Queue<int[]> q = new LinkedList<>();
        q.add(new int[]{0, 0});
        visited[0] = -1;
        while (!q.isEmpty()) {
            int[] node = q.poll();
            if (node[0] != 0) visited[node[0]] = node[1];
            for (int i = 0; i < path.get(node[0]).size(); i++) {
                int loca = path.get(node[0]).get(i)[0];
                int dis = path.get(node[0]).get(i)[1];
                if (visited[loca] == 0) {
                    q.add(new int[]{loca, dis + node[1]});
                }
            }
        }

        long maxlen = -2;
        int id = 0;
        for (int i = 0; i < n; ++i) {
            if (visited[i] > maxlen) {
                maxlen = visited[i];
                id = i;
            }
        }

        visited = new long[n];
        q.add(new int[]{id, 0});
        visited[id] = -1;
        while (!q.isEmpty()) {
            int[] node = q.poll();
            if (node[0] != id) visited[node[0]] = node[1];
            for (int i = 0; i < path.get(node[0]).size(); i++) {
                int loca = path.get(node[0]).get(i)[0];
                int dis = path.get(node[0]).get(i)[1];
                if (visited[loca] == 0) {
                    q.add(new int[]{loca, dis + node[1]});
                }
            }
        }

        maxlen = -1;
        for (int i = 0; i < n; ++i) {
            if (maxlen < visited[i]) {
                maxlen = visited[i];
            }
        }

        System.out.println(10 * maxlen + ((1 + maxlen) * maxlen >> 1));
    }
}

